package publisher;



import aimpublisher.AIMPublisher;
import configuration.EntryExists;
import configuration.NoEntryException;

import twitter4j.*;
// STATE_INITIAL = 0
// STATE_ACQUIRING = 1
// STATE_HASUSER = 2
// STATE_HASPASSWORD = 3
public class Twitter extends Publisher {
twitter4j.Twitter twitter;
	
	public Twitter() { 
	}
public String run(String ms) {
	String user = null;
	String password = null;
	String content = null;
	// Retrieve the e-mail address
	if (AIMPublisher.getConfiguration().isEntry("Twitter",
			this.getIdentity(), "user")) {
		try {
			user = AIMPublisher.getConfiguration().getEntry("Twitter",
					this.getIdentity(), "user");
		} catch (final NoEntryException e) {
			// can't happen
		}
	}

	// Retrieve the password
	if (AIMPublisher.getConfiguration().isEntry("Twitter",
			this.getIdentity(), "password")) {
		try {
			password = AIMPublisher.getConfiguration().getEntry("Twitter",
					this.getIdentity(), "password");
		} catch (final NoEntryException e) {
			// can't happen
		}
	}

	// Retrieve the content
	if (AIMPublisher.getConfiguration().isEntry("Twitter",
			this.getIdentity(), "content")) {
		try {
			content = AIMPublisher.getConfiguration().getEntry("Twitter",
					this.getIdentity(), "content");
		} catch (final NoEntryException e) {
			// can't happen
		}
	}

	final int oldState = this.getState();

	if (user == null) {
		this.setState(1); // STATE_ACQUIRING
	} else if (password == null) {
		this.setState(2); // STATE_HASUSER
	} else if (content == null) {
		this.setState(3); // STATE_HASPASSWORD
	}
	// STATE_INITIAL -> STATE_ACQUIRING
	if ((oldState == 0) && (this.getState() == 1)) {
		return "Please specify a Twitter username.";
	}

	// STATE_INITIAL -> STATE_HASUSER
	if ((oldState == 0) && (this.getState() == 2)) {
		return "Please specify a password.";
	}

	// STATE_INITIAL -> STATE_HASPASSWORD
	if ((oldState == 0) && (this.getState() == 3)) {
		twitter = new twitter4j.Twitter(user, password);
		return "Please specify a message.";
	}
	switch (this.getState()) {
	case 1: // STATE_ACQUIRING
		// Store the e-mail address for later retrieval
		user = ms;
		try {
			if (AIMPublisher.getConfiguration().isEntry("Twitter",
					this.getIdentity(), "user")) {
				AIMPublisher.getConfiguration().changeEntry("Twitter",
						this.getIdentity(), "user", user);
			} else {
				AIMPublisher.getConfiguration().addEntry("Twitter",
						this.getIdentity(), "user", user);
			}
		} catch (final EntryExists e) {
			// can't happen
		} catch (final NoEntryException e) {
			// can't happen
		}

		return "Please specify a password.";
	case 2: // STATE_HASUSER
		// Store the password for later retrieval
		password = ms;
		try {
			if (AIMPublisher.getConfiguration().isEntry("Twitter",
					this.getIdentity(), "password")) {
				AIMPublisher.getConfiguration().changeEntry("Twitter",
						this.getIdentity(), "password", password);
			} else {
				AIMPublisher.getConfiguration().addEntry("Twitter",
						this.getIdentity(), "password", password);
			}
		} catch (final EntryExists e) {
			// can't happen
		} catch (final NoEntryException e) {
			// can't happen
		}
		

			twitter = new twitter4j.Twitter(user, password);


		return "Please specify a message.";
	case 3: // STATE_HASPASSWORD
		content = ms;
		   try {
			   twitter.update(content);
	        } catch (TwitterException te) {
	        	if (AIMPublisher.getConfiguration().isEntry("Twitter",
						this.getIdentity(), "user")) {
					try {
						AIMPublisher.getConfiguration().removeEntry("Twitter",
								this.getIdentity(), "user");
					} catch (final NoEntryException e) {
						// can't happen
					}
				}
				if (AIMPublisher.getConfiguration().isEntry("Twitter",
						this.getIdentity(), "password")) {
					try {
						AIMPublisher.getConfiguration().removeEntry("Twitter",
								this.getIdentity(), "password");
					} catch (final NoEntryException e) {
						// can't happen
					}
				}
	        	return "There was an error posting to Twitter, Please Try again.";
	        }
	        this.setState(0);
	        return "Successfully posted to Twitter";
	}

	return null;
}


}